package com.vader.admin.service.impl;

import com.vader.admin.client.DistributeIdClient;
import com.vader.admin.dao.BaseGroupUserRelationDao;
import com.vader.admin.entity.DO.BaseGroupUserRelation;
import com.vader.admin.service.BaseGroupUserRelationService;
import com.vader.common.entity.CommonResponse;
import com.vader.common.exception.BadRequestException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Date;
import java.time.LocalDate;
import java.util.Objects;

/**
 * 组织用户关系表
 * (BaseGroupUserRelation)表服务实现类
 *
 * @author Vader
 * @since 2021-10-06 12:46:39
 */
@Service("baseGroupUserRelationService")
public class BaseGroupUserRelationServiceImpl implements BaseGroupUserRelationService {
    @Resource
    private BaseGroupUserRelationDao baseGroupUserRelationDao;
    @Resource
    private DistributeIdClient distributeIdClient;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public BaseGroupUserRelation queryById(Long id) {
        return this.baseGroupUserRelationDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param baseGroupUserRelation 筛选条件
     * @param pageRequest           分页对象
     * @return 查询结果
     */
    @Override
    public Page<BaseGroupUserRelation> queryByPage(BaseGroupUserRelation baseGroupUserRelation, PageRequest pageRequest) {
        long total = this.baseGroupUserRelationDao.count(baseGroupUserRelation);
        return new PageImpl<>(this.baseGroupUserRelationDao.queryAllByLimit(baseGroupUserRelation, pageRequest), pageRequest, total);
    }

    /**
     * 用户加入组织
     *
     * @param baseGroupUserRelation 实例对象
     * @return 实例对象
     */
    @Override
    public BaseGroupUserRelation insert(BaseGroupUserRelation baseGroupUserRelation) {
        BaseGroupUserRelation groupUserRelation = baseGroupUserRelationDao.queryByUidAndGid(baseGroupUserRelation.getGid(), baseGroupUserRelation.getUid());
        if (!Objects.isNull(groupUserRelation)) {
            throw new BadRequestException("请勿重复加入");
        }
        CommonResponse<Long> id = distributeIdClient.getId();
        if (id.getCode() != 0) {
            throw new BadRequestException("用户组织关系id生成异常");
        }
        baseGroupUserRelation.setId(id.getData());
        baseGroupUserRelation.setCreateTime(Date.valueOf(LocalDate.now()));
        baseGroupUserRelation.setUpdateTime(Date.valueOf(LocalDate.now()));
        this.baseGroupUserRelationDao.insert(baseGroupUserRelation);
        return baseGroupUserRelation;
    }

    /**
     * 修改数据
     *
     * @param baseGroupUserRelation 实例对象
     * @return 实例对象
     */
    @Override
    public BaseGroupUserRelation update(BaseGroupUserRelation baseGroupUserRelation) {
        this.baseGroupUserRelationDao.update(baseGroupUserRelation);
        return this.queryById(baseGroupUserRelation.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.baseGroupUserRelationDao.deleteById(id) > 0;
    }
}
